package com.zntg.schedule.FileScheduledTask;
import com.zntg.bo.CrmAllFileBo;
import com.zntg.mapper.odsdb.CrmAllFileMapper;
import com.zntg.utils.ComponentFactory;
import com.zntg.utils.DateUtil;
import com.zntg.utils.DateUtils;
import com.zntg.utils.file.TakeFilePathAndName;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
 *
* @Description: 定时将认购书已生成，但是文件没有生成的再次定时生成一次
* @Author: zhengyunfei
* @Date: 2019/6/21
* @time:13:21
*/
@Component
public class FileScheduledTask {
    @Value("${server.port}")
    String port;
    /**电子签章生成路径*/
    @Value("${template.targetPath}")
    String targetPath;
    private static final org.slf4j.Logger Logger = LoggerFactory.getLogger(FileScheduledTask.class);
   @Scheduled(cron = "0 0 3 * * ?")   //每天晚上3点执行
   /*@Scheduled(fixedRate = 50000)*/
    public void task1() {
        try {
            System.out.println("定时获取已生成的所有文件（本月的）FileScheduledTask========执行时间=="+ DateUtils.getCurrentDate());
            ArrayList<File> files=TakeFilePathAndName.getListFiles(targetPath);
            System.out.println("总文件数=========================================="+files.size());
            CrmAllFileMapper crmAllFileMapper = ComponentFactory.getBean(CrmAllFileMapper.class);
           for(int i=0;i<files.size();i++){
               String fileName=files.get(i).getName();
               String fileUrl=files.get(i).getPath();
               String month= DateUtil.getMonth();
               if(fileUrl.contains(month)){
                   Map<String,Object> queryMap=new HashMap<>();
                   queryMap.put("fileName",fileName);
                   int count=crmAllFileMapper.count(queryMap);
                   CrmAllFileBo crmAllFileBo=new CrmAllFileBo();
                   crmAllFileBo.setFileUrl(fileUrl);
                   crmAllFileBo.setFileName(fileName);
                   if(count==0){
                       crmAllFileMapper.add(crmAllFileBo);
                   }
               }

           }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {

        }
    }

}

